﻿using Dapper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UserCenter.BLL;
using UserCenter.Models;

namespace UserCenter.TreeViewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (var conn = DatabaseHelper.Get())
            {
                var rootMembers = conn.Query<Member>(@"SELECT 
                                                      mr.* 
                                                    FROM
                                                      (SELECT 
                                                        m.MemberId AS MemberId 
                                                      FROM
                                                        member AS m 
                                                        LEFT JOIN member_layer AS ml 
                                                          ON m.`MemberId` = ml.`MemberId` 
                                                        LEFT JOIN member_layer AS ml2 
                                                          ON m.`MemberId` = ml2.`ParentId` 
                                                      WHERE ml.`MemberId` IS NULL 
                                                        AND ml2.`MemberId` IS NOT NULL 
                                                      GROUP BY m.MemberId) AS mt 
                                                      LEFT JOIN member AS mr 
                                                        ON mt.MemberId = mr.MemberId ").AsList();
            }
        }
    }
}
